// pages/task/task-details/task-details.js
const config = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskDetails: [],
    receiveStatus: false,
    contactStatus: false,
    phoneStatus: false,
    isReceive: false,
    descStatus: false,
    codeStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let taskDetailsUrl = config.taskDetailsUrl;
    wx.request({
      url: 'https://api.mokyun.com/express/task/detail',
      method: 'POST',
      data: {
        task_id: options.taskid
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: result => {
        // console.log(result)
        let pic = result.data.task_pic;
        pic = `https://cdn1-open.mokyun.com/${pic}`
        this.setData({
          taskDetails: result.data,
          pic:pic
        })
      }
    });
    console.log(getCurrentPages())
  },

  receiveTask: function() {
    let that = this;
    let certStatus = that.data.certStatus;
    // console.log('认证状态' + certStatus)
    let defaultSex = that.data.defaultSex; //用户认证的性别
    // console.log('认证的性别' + defaultSex)
    let sex = that.data.taskDetails.sex; //任务要求的性别
    // console.log('任务要求的性别' + sex);
    // console.log(that.data.taskDetails.id)
    if (certStatus != 2) {
      wx.showModal({
        title: '审核状态',
        content: '抱歉，您未审核或审核未通过',
        confirmText: '前往认证',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/certification/certification',
            })
          }
        }
      })

    } else if (sex != '2') {
      if (defaultSex == '1') {
        if (defaultSex != sex) {
          wx.showToast({
            title: '只有女生能领',
            icon: 'none'
          })
        }
      } else if (defaultSex == '0') {
        if (defaultSex != sex) {
          wx.showToast({
            title: '只有男生能领',
            icon: 'none'
          })
        }
      }
    }
    wx.request({
      url: config.receiveTaskUrl,
      method: 'POST',
      data: {
        task_id: that.data.taskDetails.id
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        'APPID': 28
      },
      success: (res) => {
        if (res.data == 'success') {
          wx.showToast({
            title: '领取成功',
            icon: 'none',
            duration: 1500,
            success: (res) => {
              that.setData({
                contactStatus: true,
                phoneStatus: true,
                receiveStatus: true,
                isReceive: true,
                descStatus: true,
                codeStatus: true
              })
            }
          })
        } else if (res.data.result == 'You can not receive yourself task!') {
          wx.showToast({
            title: '对不起，您不能领取自己的任务',
            icon: 'none'
          })
        }
      }
    })

  },

  onShow: function() {
    let that = this;
    wx.request({
      url: config.userInfo,
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        that.setData({
          certStatus: res.data.status,
          defaultSex: res.data.sex
        })
      }
    })
  },
  onUnload:function(){
    console.log(1)
    wx.redirectTo({
      url: '/pages/index/index',
    })
  }
})